﻿using System.Collections.Generic;
using System.Linq;

namespace CRMS.Models
{
    public class StudentRep
    {
        private StoreDBDataContext storeDB = new StoreDBDataContext();

        //
        //Query Methods
        /// <summary>
        /// 获取所有学生
        /// </summary>
        /// <returns>返回所有学生的集合</returns>
        public IQueryable<Student> getStudents()
        {
            return from s in storeDB.Students select s;
        }
        /// <summary>
        /// 通过学生的ID获取特定学生实例
        /// </summary>
        /// <param name="id">学生id</param>
        /// <returns>返回学生实例</returns>
        public Student getStudentByID(string id)
        {
            return storeDB.Students.SingleOrDefault(s => s.StudentId == id);
        }
        /// <summary>
        /// 通过学生的名字和编号察看数据库中是否存在该实例
        /// </summary>
        /// <param name="name">学生名字</param>
        /// <param name="id">学生编号</param>
        /// <returns>返回一个boolean值，表明该实例是否存在</returns>
        public bool HasStudent(string name, string id)
        {
            return storeDB.Students.Any(s => s.StudentId == id && s.StudentName == name);
        }

        public IQueryable<Student> getStudentByDID(string dID)
        {
            return from s in storeDB.Students
                   where s.DepartmentId == dID
                   select s;
        }

        public bool Prompt(string id)
        {
            return storeDB.ToUseClassrooms.Any(p => p.UserId == id && p.UseStatus == '1');
        }
        public bool NotAgree()
        {
            return storeDB.ToUseClassrooms.Any(n => n.UseStatus == '0');
        }
        /// <summary>
        /// 通过学生姓名获取学生信息
        /// </summary>
        /// <param name="name">学生姓名</param>
        /// <returns>学生信息</returns>
        public IQueryable<Student> getStudentsByName(string name)
        {
            return storeDB.Students.Where(s => s.StudentName == name);
        }
        /// <summary>
        /// 通过学院名称获取该学院的的学生信息
        /// </summary>
        /// <param name="dname">学院名称</param>
        /// <returns>获取该学院的学生信息</returns>
        public IQueryable<Student> getStudentsByDName(string dname)
        {
            string departmentId = null;
            var temp = storeDB.Departments.SingleOrDefault(dp => dp.DepartmentName == dname);
            if (temp != null)
            {
                departmentId = temp.DepartmentId;
                return storeDB.Students.Where(s => s.DepartmentId == departmentId);
            }
            else
            {
                return null;
            }
        }
        //
        //Insert / Delete
        /// <summary>
        /// 向数据库中添加一个学生实例
        /// </summary>
        /// <param name="s">学生对象</param>
        public void Add(Student s)
        {
            storeDB.Students.InsertOnSubmit(s);
        }
        /// <summary>
        /// 从数据库中删除一个学生实例
        /// </summary>
        /// <param name="s">学生对象</param>
        public void Delete(Student s)
        {
            storeDB.Students.DeleteOnSubmit(s);
        }

        //
        //Persistence
        /// <summary>
        /// 把更改保存到数据库
        /// </summary>
        public void Save()
        {
            storeDB.SubmitChanges();
        }
    }
}
